<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>添加战队</title>
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=no" name="format-detection" />

    <link rel="stylesheet" href="css/demo.css" />
    <script src="js/ydui.flexible.js"></script>
    <style>
        .cell-left {
            width: 20%
        }
    </style>
</head>

<body>
    <section class="g-flexview">
        <section class="g-scrollview">
            <div class="m-cell demo-small-pitch" style="margin-bottom: 0">
                <div class="cell-item">
                    <div class="cell-left">战队ID</div>
                    <div class="cell-right">
                        <input type="text" id="gameId" class="cell-input" value="" placeholder="支持靓号" autocomplete="off" />
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">战队昵称</div>
                    <div class="cell-right">
                        <input type="text" id="gameName" class="cell-input" value="" placeholder="战队昵称" autocomplete="off" />
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">战队地址</div>
                    <div class="cell-right cell-arrow">
                        <input type="text" id="gameaddress" readonly class="cell-input" placeholder="请选择网吧地址" />
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">详细地址</div>
                    <div class="cell-right">
                        <input type="text" id="teamAddress" class="cell-input" value="" placeholder="请输入详细地址" autocomplete="off" />
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">战队图片</div>
                    <div class="cell-right" style="justify-content: flex-start; padding: 10px 0">
                        <img id="uploadImg" src="images/noPic.png" width="160" alt="战队图片" />
                        <a href="javascript:;" style="position:absolute;right:5%;" class="btn btn-primary" onclick="clickUpImg()">上传图片</a>
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">战队简介</div>
                    <div class="cell-right" style="justify-content: flex-start; padding: 10px 0">
                        <textarea class="cell-textarea" id="gameReferral" placeholder="请输入战队简介"></textarea>
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">队长</div>
                    <div class="cell-right">
                        <input type="text" class="cell-input" id="captain" value="" placeholder="输入管理员手机号" autocomplete="off" />
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">鸡蛋数</div>
                    <div class="cell-right">
                        <input type="text" class="cell-input" id="eggNum" value="0" readonly style="width:30%" placeholder="默认展示当前鸡蛋数" autocomplete="off" />
                        <a href="javascript:;" class="btn btn-primary add_egg margin_r5" id="J_Confirm">加鸡蛋</a>
                        <a href="javascript:;" class="btn btn-primary add_egg" id="J_Confirm_subtract">减鸡蛋</a>
                    </div>
                </div>
                <div class="cell-item">
                    <div class="cell-left"></div>
                    <div class="cell-right" id="eggBz" style="justify-content: flex-end;">注：加减鸡蛋无需点击保存</div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">邀请码</div>
                    <div class="cell-right">
                        <input type="text" class="cell-input" id="InvitationCode" value="" placeholder="请输入邀请码" autocomplete="off" />
                    </div>
                </div>
                <!--<div class="cell-item">-->
                    <!--<div class="cell-left"></div>-->
                    <!--<div class="cell_right_bz">注：按对员参与赛场的利润给战队队长结算</div>-->
                <!--</div>-->
                <div class="cell-item">
                    <div class="cell-left">提成设置</div>
                    <div class="cell-right">
                        <input type="text" class="cell-input" value="" id="promote" placeholder="按照利润进行提成，百分之五十请输入0.5" autocomplete="off" />
                    </div>
                </div>
            </div>
            <div class="m-button">
                <!--<div class="cell_right_bz" style="color: red;font-size: .3rem">注：加减鸡蛋无需点击保存</div>-->
                <button type="button" id="saveBtn" onclick="saveBtnTeam()"  class="btn-block btn-primary">保存</button>
            </div>

        </section>

    </section>
    <script src="js/jquery.min.js"></script>
    <script src="js/ydui.js"></script>
    <script src="js/ydui.citys.js"></script>
    <script src="js/vue-spinner-master/dist/vue-spinner.js"></script>
    <script src="js/layer/layer.js"></script>
</body>
<script>
    /* 获取头部信息 */
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return r[2];
        }
    };
    var token = GetQueryString('token');
    var appType = GetQueryString('appType');
    var addegg = GetQueryString('addegg');
    var rowId = GetQueryString('rowId');
    var clubId, clubName, uploadImgUrl;

    function loadClub() {
        $.ajax({
            url: "/appClub/clubDeatil",
            type: "post",
            data: {
                token: token,
            },
            dataType: "json",
            success: function(data) {
                clubId = data.data.rowId;
                clubName = data.data.clubName;
            },
            error: function(data) {}
        });
    }
    loadClub();


    if (addegg == 0) {
        //添加战队
        $("#J_Confirm").hide();
        $("#J_Confirm_subtract").hide();
        $("#eggBz").hide();
    } else {
        //编辑战队
        function loadTeamDetail() {
            $.ajax({
                url: "/appTeam/pubgTeamDetail",
                type: "post",
                data: {
                    token: token,
                    userRole: "30",
                    rowId: rowId
                },
                dataType: "json",
                success: function(data) {
                    if (data.code == '0') {
                        console.log(data);
                        $("#gameId").val(data.data.teamNo);
                        $("#gameName").val(data.data.teamName);
                        $("#gameaddress").val(data.data.placeProvince + " " + data.data.placeCity + " " + data.data.placeDistrict);
                        $("#teamAddress").val(data.data.teamAddress);
                        $("#gameReferral").val(data.data.teamIntro);
                        $("#captain").val(data.data.captainAccount);
                        $("#InvitationCode").val(data.data.inviteCode);
                        $("#eggNum").val(data.data.teamBalance);
                        $("#promote").val(data.data.settlementRatio);
                        uploadImgUrl = data.data.teamPhoto;
                        $("#uploadImg").attr('src', uploadImgUrl);
                    }
                },
                error: function(data) {
                    layer.msg("战队详情获取失败");
                }
            });
        }
        loadTeamDetail();
    }
    //省市区地址联动
    var $address = $('#gameaddress');
    $address.citySelect();
    $address.on('click', function() {
        $address.citySelect('open');
    });

    $address.on('done.ydui.cityselect', function(ret) {
        /* 省：ret.provance */
        /* 市：ret.city */
        /* 县：ret.area */
        $(this).val(ret.provance + ' ' + ret.city + ' ' + ret.area);
    });
    bool = true;//锁住
    /* 添加鸡蛋 */
    $('#J_Confirm').on('click', function() {
        layer.confirm('<p class="c_t_egg">增加鸡蛋</p><input  type="number" class="cell-input num_bor_s" id="addrgg" value="" placeholder="请输入数量" autocomplete="off" />', {
                closeBtn: 0,
                title: false
            },
            function() {
                if(!bool) {
                    var eggnum = $("#addrgg").val();
                    var bool = false;  //加个锁
                    console.log(1);
                    /*调取接口加入*/
                    $.ajax({
                        url: "/appClub/updateTeamBalance",
                        type: "post",
                        data: {
                            token: token,
                            tramId: rowId,
                            optType: 1,
                            optMoney: eggnum
                        },
                        dataType: "json",
                        success: function (res) {
                            layer.msg(res.message);
                            if (res.code == '0') {
                                layer.msg('确认成功', {
                                    icon: 1
                                });
                                console.log(res);
                                location.reload();
                                return;
                            } else {
                                console.log(res);
                                layer.msg(res.message);
                            }
                        },
                        error: function (res) {
                            //console.log(res);
                            layer.msg('调取失败');
                        }
                    });
                }
            });
    });
    /*减鸡蛋*/
    $('#J_Confirm_subtract').on('click', function() {
        layer.confirm('<p class="c_t_egg">减鸡蛋</p><input  type="number" class="cell-input num_bor_s" id="minusegg" value="" placeholder="请输入数量" autocomplete="off" />', {
                closeBtn: 0,
                title: false
            },
            function() {
                var eggnum = $("#minusegg").val();
                /*调取接口加入*/
                $.ajax({
                    url: "/appClub/updateTeamBalance",
                    type: "post",
                    data: {
                        token: token,
                        tramId: rowId,
                        optType: 2,
                        optMoney: eggnum
                    },
                    dataType: "json",
                    success: function(res) {
                        layer.msg(res.message);
                        if (res.code == '0') {
                            layer.msg('确认成功', {
                                icon: 1
                            });
                            console.log(res);
                            location.reload();
                            return;
                        } else {
                            console.log(res);
                            layer.msg(res.message);
                        }
                    },
                    error: function() {
                        layer.msg('接口调取失败');
                    }
                });
            });
    });

    function clickUpImg() {
        if (appType == 'ios') {
            //ios 上传图片
            window.webkit.messageHandlers.iosuploadimg.postMessage('');
        } else {
            window.android.uploadImg();
        }
    }

    //ios上传图片回填方法
    function uploadSuccessByIos(iosUploadImgUrl) {
        $("#uploadImg").attr('src', iosUploadImgUrl);
        uploadImgUrl = iosUploadImgUrl;
    }
    //安卓上传图片回填方法
    function uploadImgSuccess(aUploadImgUrl) {
        $("#uploadImg").attr('src', aUploadImgUrl);
        uploadImgUrl = aUploadImgUrl;
    }

    function saveBtnTeam() {
        var gameId = $("#gameId").val();
        var gameName = $("#gameName").val();
        var gameaddress = $("#gameaddress").val();
        var addressArr = gameaddress.split(' ');
        var teamAddress = $("#teamAddress").val();
        var gameReferral = $("#gameReferral").val();
        var captain = $("#captain").val();
        var eggNum = $("#eggNum").val();
        var InvitationCode = $("#InvitationCode").val();
        var promote = $("#promote").val();
        var teamAddress = $("#teamAddress").val();

        if (gameId == "" || gameId == null) {
            layer.msg('请输入战队Id');
            return;
        } else
        if (gameName == "") {
            layer.msg('请输入战队昵称');
            return;
        } else if (gameaddress == "") {
            layer.msg('请选择省市区');
            return;
        } else if (gameaddress == "") {
            layer.msg('请输入战队昵称');
            return;
        } else if (teamAddress == "") {
            layer.msg('请输入详细地址');
            return;
        } else if (gameReferral == "") {
            layer.msg('请输入战队简介');
            return;
        } else if (captain == "") {
            layer.msg('请输入队长');
            return;
        } else if (eggNum == "") {
            layer.msg('请输入鸡蛋数');
            return;
        } else if (InvitationCode == "") {
            layer.msg('请输入邀请码');
            return;
        } else if (promote == "") {
            layer.msg('请输入提升百分比');
            return;
        } else if (uploadImgUrl == "") {
            layer.msg('请上传图片');
            return;
        }
        if (InvitationCode.length > 6) {
            layer.msg('邀请码不能大于6位');
            return;
        }
        if (promote >= 1) {
            layer.msg('提升百分比必须小于1');
            return;
        }
        $.ajax({
            url: "/appTeam/addPubgTeam",
            type: "post",
            data: {
                token: token,
                teamNo: gameId,
                rowId: rowId,
                teamName: gameName,
                placeProvince: addressArr[0],
                placeCity: addressArr[1],
                placeDistrict: addressArr[2],
                teamAddress: teamAddress,
                teamIntro: gameReferral,
                captainAccount: captain,
                inviteCode: InvitationCode,
                clubId: clubId,
                clubName: clubName,
                teamBalance: eggNum,
                settlementRatio: promote,
                teamPhoto: uploadImgUrl
            },
            dataType: "json",
            success: function(data) {
                layer.msg(data.message);
                if (data.code == '0') {
                    if (appType == 'ios') {
                        window.webkit.messageHandlers.returnBack.postMessage('');
                    }else if(appType == 'android'){
                        window.android.goBack();
                    }
                } else {
                    layer.msg(data.message);
                }
            },
            error: function(data) {
                layer.msg('接口调取失败' + data);
            }
        });
    }
</script>

</html>